package com.haochang.chunkvod.controller.activity.hostess;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.base.OnBaseItemClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ComparatorUtil;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.ConsumeQueryInfo;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;
import com.haochang.chunkvod.view.vod.title.TitleView;

import java.util.ArrayList;
import java.util.List;

/**
 *         created at 2016/8/29 10:27
 *         消费列表页面
 */
public class ConsumeQueryActivity extends BaseActivity
{
	private BaseTextView tv_room_num, tv_money, tv_p_count, tv_money_avg, tv_time;
	private BaseListView query_list;
	private BaseTextView tv_clicked;//记录被按下的textview
	private BaseTextView currentOrderView;//当前选中的排序按钮
	private BaseTextView currentBottomView;//当前选中的金额区域

	//金额区域  默认全部
	private int moneyInterval = 0;
	private QuickAdapter<ConsumeQueryInfo> adapter;
	private ArrayList<ConsumeQueryInfo> infos = new ArrayList<ConsumeQueryInfo>();
	private ArrayList<ConsumeQueryInfo> hostInfos = new ArrayList<ConsumeQueryInfo>();
	private HostessData hostessData;
	private String sortBy = "consume";

	@Override
	public void receiveParam()
	{

	}

	@Override
	public void initData()
	{

	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_consume_query);
		hostessData = new HostessData(this);
		TitleView titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle("消费查询").setRightIconVISIBLE(false).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
				animRightToLeft();
			}

			@Override
			public void onDoneClick()
			{

			}

		});
		LinearLayout linear_room_num = (LinearLayout) findViewById(R.id.linear_room_num);
		LinearLayout linear_money = (LinearLayout) findViewById(R.id.linear_money);
		LinearLayout linear_p_count = (LinearLayout) findViewById(R.id.linear_p_count);
		LinearLayout linear_money_avg = (LinearLayout) findViewById(R.id.linear_money_avg);
		LinearLayout linear_time = (LinearLayout) findViewById(R.id.linear_time);
		BaseTextView tv_query_all = (BaseTextView) findViewById(R.id.tv_query_all);
		BaseTextView tv_query_ten = (BaseTextView) findViewById(R.id.tv_query_ten);
		BaseTextView tv_query_five = (BaseTextView) findViewById(R.id.tv_query_five);
		BaseTextView tv_query_one = (BaseTextView) findViewById(R.id.tv_query_one);
		BaseTextView tv_query_five_t = (BaseTextView) findViewById(R.id.tv_query_five_t);
		tv_room_num = (BaseTextView) findViewById(R.id.tv_room_num);
		tv_money = (BaseTextView) findViewById(R.id.tv_money);
		tv_p_count = (BaseTextView) findViewById(R.id.tv_p_count);
		tv_money_avg = (BaseTextView) findViewById(R.id.tv_money_avg);
		tv_time = (BaseTextView) findViewById(R.id.tv_time);

		linear_room_num.setOnClickListener(orderBaseClickListener);
		linear_money.setOnClickListener(orderBaseClickListener);
		linear_p_count.setOnClickListener(orderBaseClickListener);
		linear_money_avg.setOnClickListener(orderBaseClickListener);
		linear_time.setOnClickListener(orderBaseClickListener);
		tv_query_all.setOnClickListener(bottomListener);
		tv_query_ten.setOnClickListener(bottomListener);
		tv_query_five.setOnClickListener(bottomListener);
		tv_query_one.setOnClickListener(bottomListener);
		tv_query_five_t.setOnClickListener(bottomListener);

		refreshArrowView(tv_money);//初始化箭头
		refreshMoneyInterval(tv_query_all);//初始化下方金额

		query_list = (BaseListView) findViewById(R.id.query_list);
		requestConsumeList();
	}

	/**
	 * 刷新箭头
	 * @param view 传入点击的view
	 */
	private void refreshArrowView(BaseTextView view)
	{
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.up2);
		int height = bitmap.getHeight();
		int width = bitmap.getWidth();
		if (currentOrderView != null)
		{
			currentOrderView.setTextColor(getResources().getColor(R.color.color_737373));
			Drawable drawable = getResources().getDrawable(R.drawable.up2);
			if (null != drawable)
			{
				drawable.setBounds(0, 0, width, height);
				currentOrderView.setCompoundDrawables(null, null, drawable, null);
				currentOrderView.setCompoundDrawablePadding(DipPxConversion.dipToPx(ConsumeQueryActivity.this, 4));
			}
		}
		view.setTextColor(getResources().getColor(R.color.color_f04e39));
		Drawable drawable1 = getResources().getDrawable(R.drawable.ic_triangle_red);
		if (drawable1 != null)
		{
			drawable1.setBounds(0, 0, width, height);
			view.setCompoundDrawables(null, null, drawable1, null);
			view.setCompoundDrawablePadding(DipPxConversion.dipToPx(ConsumeQueryActivity.this, 4));
			currentOrderView = view;
		}
	}

	/**
	 * 根据金额区域 获取对应的view
	 * @param view 底部金额view
	 */
	private void refreshMoneyInterval(BaseTextView view)
	{
		if (currentBottomView != null)
		{
			currentBottomView.setTextColor(getResources().getColor(R.color.color_282828));
			currentBottomView.setBackgroundColor(getResources().getColor(R.color.white));
		}
		view.setTextColor(getResources().getColor(R.color.white));
		view.setBackgroundColor(getResources().getColor(R.color.color_f04e39));
		currentBottomView = view;
	}

	private OnBaseClickListener orderBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.linear_room_num:
				tv_clicked = tv_room_num;
				infos = ComparatorUtil.sortByStringAttr(infos, sortBy = "roomNum", ComparatorUtil.SortType.ASC);
				break;
			case R.id.linear_money:
				tv_clicked = tv_money;
				infos = ComparatorUtil.sortByIntAttr(infos, sortBy = "consume");
				break;
			case R.id.linear_p_count:
				tv_clicked = tv_p_count;
				infos = ComparatorUtil.sortByIntAttr(infos, sortBy = "customerCount");
				break;
			case R.id.linear_money_avg:
				tv_clicked = tv_money_avg;
				infos = ComparatorUtil.sortByIntAttr(infos, sortBy = "consumeAvg");
				break;
			case R.id.linear_time:
				tv_clicked = tv_time;
				infos = ComparatorUtil.sortByIntAttr(infos, sortBy = "consumeTime");
				break;
			}
			try
			{
				adapter.replaceAll(infos);
				refreshArrowView(tv_clicked);
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	};

	private OnBaseClickListener bottomListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			refreshMoneyInterval((BaseTextView) v);
			//金额限制
			final int rmb100000 = 100000;
			final int rmb50000 = 50000;
			final int rmb10000 = 10000;
			final int rmb5000 = 5000;
			switch (v.getId())
			{
			case R.id.tv_query_all:
				moneyInterval = 0;
				break;
			case R.id.tv_query_ten:
				moneyInterval = rmb100000;
				break;
			case R.id.tv_query_five:
				moneyInterval = rmb50000;
				break;
			case R.id.tv_query_one:
				moneyInterval = rmb10000;
				break;
			case R.id.tv_query_five_t:
				moneyInterval = rmb5000;
				break;
			}
			infos.clear();
			//根据数量显示,后台返回单位：分
			for (ConsumeQueryInfo info : hostInfos)
			{
				if (info.getConsume() / 100 >= moneyInterval)
				{
					infos.add(info);
				}
			}
			adapter.replaceAll(infos);
		}
	};

	/**
	 * 请求消费列表
	 */
	private void requestConsumeList()
	{
		hostessData.requestConsumeList(new HostessData.ResultResponseList<ConsumeQueryInfo>()
		{
			@Override
			public void onRequestSuccess(List<ConsumeQueryInfo> list)
			{
				if (!CollectionUtils.isEmpty(list))
				{
					infos.clear();
					infos.addAll(list);
					hostInfos.addAll(list);
					showData();
				} else
				{
					ToastUtils.showText(ConsumeQueryActivity.this, "暂无消费信息!");
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
			}
		});
	}

	/**
	 * 获取并显示数据
	 */
	private void showData()
	{
		if (!TextUtils.isEmpty(sortBy))
		{
			if (sortBy.equals("roomNum"))
			{
				infos = ComparatorUtil.sortByStringAttr(infos, sortBy);
			} else
			{
				infos = ComparatorUtil.sortByIntAttr(infos, sortBy);
			}
		}
		if (adapter != null)
		{
			adapter.replaceAll(infos);
		} else
		{
			adapter = new QuickAdapter<ConsumeQueryInfo>(this, R.layout.item_consume_query, infos)
			{
				@Override
				protected void convert(BaseAdapterHelper helper, ConsumeQueryInfo item)
				{
					Drawable drawable = null;
					if (item.getIsHonor() > 0)
					{
						drawable = getResources().getDrawable(R.drawable.big_status);
					}
					if (!TextUtils.isEmpty(item.getUsage()))
					{
						if (item.getUsage().equals("meeting"))
						{
							drawable = getResources().getDrawable(R.drawable.ic_meeting);
						} else if (item.getUsage().equals("debug"))
						{
							drawable = getResources().getDrawable(R.drawable.ic_debug);
						}
						if (drawable != null)
						{
							Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_debug);
							int height = bitmap.getHeight();
							int width = bitmap.getWidth();
							drawable.setBounds(0, 0, width, height);
							((BaseTextView) helper.getView().findViewById(R.id.tv_room_num)).setCompoundDrawables(null, null, drawable, null);
							((BaseTextView) helper.getView().findViewById(R.id.tv_room_num))
									.setCompoundDrawablePadding(DipPxConversion.dipToPx(ConsumeQueryActivity.this, 4));
						}
					}
					helper.setText(R.id.tv_room_num, item.getRoomNum());
					helper.setText(R.id.tv_money, item.getConsume() / 100 + "元");
					helper.setText(R.id.tv_p_count, String.valueOf(item.getCustomerCount()));
					helper.setText(R.id.tv_money_avg, String.valueOf(item.getCustomerCount() != 0 ? item.getConsume() / item.getCustomerCount() / 100.0 : 0));
					String time = "0";
					if (!TextUtils.isEmpty(item.getBeginTime()) && !TextUtils.isEmpty(item.getEndTime()))
					{
						time = TimeUtils.timeMinusEn(item.getEndTime(), item.getBeginTime());
					}
					helper.setText(R.id.tv_time, time);
				}
			};
			query_list.setAdapter(adapter);
			query_list.setOnItemClickListener(new OnBaseItemClickListener()
			{
				@Override
				public void onBaseItemClick(AdapterView<?> parent, View view, int position, long id)
				{
					if (infos.get(position) == null || TextUtils.isEmpty(infos.get(position).getBillId())
							|| TextUtils.isEmpty(infos.get(position).getRoomNum()))
					{
						ToastUtils.showText(ConsumeQueryActivity.this, "账单信息获取有误！");
						return;
					}
					Intent intent = new Intent(ConsumeQueryActivity.this, ConsumeInfoActivity.class);
					intent.putExtra(IntentKey.BILL_ID, infos.get(position).getBillId());
					intent.putExtra(IntentKey.ROOM_NUM, infos.get(position).getRoomNum());
					startActivity(intent);
				}
			});
		}
	}

}
